package cn.itcast.nems.student.controller.b;

import cn.itcast.nems.oldems.vo.StudentAddressListVo;
import cn.itcast.nems.student.bo.StudentAddressEditBo;
import cn.itcast.nems.student.service.StudentAddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/b/student/{studentId}/address")
@RequiredArgsConstructor
public class StudentAddressController {

    private final StudentAddressService studentAddressService;

    /**
     * 列出一个学生的所有地址
     * <p>
     * 与李勇与廉德明老师商议，地址由主要地址+附表共同组成
     *
     * @param studentId
     * @return
     */
    @GetMapping
    public ResponseEntity<List<StudentAddressListVo>> listAll(
            @PathVariable("studentId") int studentId
    ) {
        return ResponseEntity.ok(studentAddressService.listAll(studentId));
    }

    /**
     * 新增、编辑、删除地址
     * <p>
     * 操作使用action区分
     * 编辑为EDIT， 新增为ADD，删除为DELETE
     *
     * @param bo
     * @return
     */
    @PutMapping
    public ResponseEntity<Boolean> edit(@RequestBody StudentAddressEditBo bo) {
        return switch (bo.getAction()) {
            case "ADD" -> ResponseEntity.ok(studentAddressService.addStudentAddressInfo(bo));
            case "EDIT" -> ResponseEntity.ok(studentAddressService.editStudentAddressInfo(bo));
            case "DELETE" -> ResponseEntity.ok(studentAddressService.deleteStudentAddressInfo(bo));
            default -> ResponseEntity.ok(Boolean.FALSE);
        };
    }

}
